package com.cci.kangdao.controller;

import com.alibaba.fastjson.JSONObject;
import com.cci.kangdao.aop.annotation.NoDuplicateSubmission;
import com.cci.kangdao.dto.BaseResponse;
import com.cci.kangdao.dto.request.CloseWorkOrderRequest;
import com.cci.kangdao.entity.WorkOrderCloseEntity;
import com.cci.kangdao.model.DiagnoseConclusionInfo;
import com.cci.kangdao.model.condition.CloseWorkOrderCondition;
import com.cci.kangdao.model.condition.RefuseCloseWorkOrderCondition;
import com.cci.kangdao.service.WorkOrderTService;
import com.cci.kangdao.utilTool.BeanUtils;
import com.cci.kangdao.utilTool.ParameterTool;
import com.cci.kangdao.utilTool.RequestContextHolder;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 工单类controller
 *
 */
@Controller
public class WorkOrderTController {

	private Logger log = Logger.getLogger(this.getClass().getName());
	@Autowired
	private WorkOrderTService workOrderTService;

	// 服务站站长和服务站信息员和服务技师关闭工单
	@NoDuplicateSubmission
	@RequestMapping("/updateCloseWorkOrderById.aspx")
	@ResponseBody
	public BaseResponse updateCloseWorkOrderById(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		try {
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			return workOrderTService.updateCloseWorkOrderById(map);
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			return BaseResponse.error("未知错误");
		}
	}

	// 区域工程师关闭工单
	@NoDuplicateSubmission
	@RequestMapping("/areaEngineerCloseWorkOrder.aspx")
	public ModelAndView areaEngineerCloseWorkOrder(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		try {
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			return workOrderTService.areaEngineerCloseWorkOrder(map);
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);
		}
		return null;
	}

	// 接受工单
	@NoDuplicateSubmission
	@RequestMapping("/receiveWorkOrder.aspx")
	public ModelAndView receiveWorkOrder(HttpServletRequest request, HttpServletResponse response) throws Exception {
		try {
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			return workOrderTService.receiveWorkOrder(map);
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);
		}
		return null;
	}

	// 服务站拒绝工单
	@NoDuplicateSubmission
	@RequestMapping("/serviceStationRefuseWorkOrder.aspx")
	public ModelAndView serviceStationRefuseWorkOrder(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		try {
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			return workOrderTService.serviceStationRefuseWorkOrder(map);
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);
		}
		return null;
	}

	// 开始维修和维修完成接口(两个合为一个)
	@NoDuplicateSubmission
	@RequestMapping("/startRepairToEndWorkOrder.aspx")
	public ModelAndView startRepairToEndWorkOrder(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		try {
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			return workOrderTService.startRepairToEndWorkOrder(map);
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);
		}
		return null;
	}

	// 当服务技师点击开始维修按钮时调用这个接口
	// 校验该服务技师所在的服务站有没有诊断维修权限，有返回诊断工单所需数据，没有提示前端
	@RequestMapping("/checkServiceStationPower.aspx")
	public ModelAndView checkPower(HttpServletRequest request, HttpServletResponse response) throws Exception {
		try {
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			return workOrderTService.checkServiceStationPower(map);
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);
		}
		return null;
	}

	// 区域工程师远程解决服务工单功能
	@NoDuplicateSubmission
	@RequestMapping("/areaEngineerLongResolve.aspx")
	public ModelAndView areaEngineerLongResolve(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		try {
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			return workOrderTService.areaEngineerLongResolve(map);
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);
		}
		return null;
	}

	/**
	 * 诊断结论详情
	 *
	 * @return
	 */
	@RequestMapping(value = "/getDiagnoseByOrderId.aspx")
	@ResponseBody
	public BaseResponse getDiagnoseByOrderId(@RequestParam("orderId") Long orderId) {
		try {
			DiagnoseConclusionInfo info = workOrderTService.getDiagnoseByOrderId(orderId);
			Map<String, Object> map = new HashMap<>();
			map.put("diagnoseConclusionDetail",info);
			return BaseResponse.success(map);
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			return BaseResponse.error("未知错误");
		}
	}

	// 区域工程师指派服务工单给服务站
	@NoDuplicateSubmission
	@RequestMapping("/areaEngineerAssignServerStation.aspx")
	public ModelAndView areaEngineerAssignServerStation(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		try {
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			return workOrderTService.areaEngineerAssignServerStation(map);
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);
		}
		return null;
	}

	// 服务站拒绝工单(区域工程师指派服务站，服务站拒单调用这个方法)
	@NoDuplicateSubmission
	@RequestMapping("/serviceStationRefuseWorkOrderForDBU.aspx")
	public ModelAndView serviceStationRefuseWorkOrderForDBU(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		try {
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			return workOrderTService.serviceStationRefuseWorkOrderForDBU(map);
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);
		}
		return null;
	}

	// 服务技师自行诊断服务工单(DBU)
	@NoDuplicateSubmission
	@RequestMapping("/selfDiagnosticWorkOrder.aspx")
	public ModelAndView selfDiagnosticWorkOrder(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		try {
			String eAppVersion = request.getHeader("EAppVersion");
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			map.put("eAppVersion", eAppVersion);
			return workOrderTService.selfDiagnosticWorkOrder(map);
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);
		}
		return null;
	}

	// 获取自行诊断详情接口(DBU)
	@RequestMapping("/getSelfDiagnosticDetail.aspx")
	public ModelAndView getSelfDiagnosticDetail(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		try {
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			return workOrderTService.getSelfDiagnosticDetail(map);
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);
		}
		return null;
	}

	// CCEC400审核不通过驳回时 根据工单来源让对应用户更新工单信息 (用户报单让用户更新 服务站报单让服务站站长信息员更新)
	@NoDuplicateSubmission
	@RequestMapping("/updateWorkOrderMsg.aspx")
	public ModelAndView updateWorkOrderMsg(HttpServletRequest request, HttpServletResponse response) throws Exception {
		try {
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			return workOrderTService.updateWorkOrderMsg(map);
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);
		}
		return null;
	}

	@RequestMapping("updateWorkOrderStatus.aspx")
	public ModelAndView updateWorkOrderStatus(HttpServletRequest request, HttpServletResponse response) {
		try {
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			workOrderTService.updateWorkOrderStatus(map);
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			ParameterTool.writeErrorResponse(response);
		}
		return null;
	}

	@RequestMapping("/getWorkOrderForMT.aspx")
	public String getWorkOrderListForMT(HttpServletRequest request, HttpServletResponse response) {
		try {
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("request", request);
			map.put("response", response);
			workOrderTService.getWorkOrderForMT(map);
		} catch (Exception e) {
			// TODO: handle exception
			log.error(e.getMessage(), e);
			ParameterTool.writeErrorResponse(response);
		}
		return null;
	}

	@RequestMapping("/clickRepairForDBU.aspx")
	public ModelAndView clickRepairForDBU(HttpServletRequest request, HttpServletResponse response) throws Exception {
		try {
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			return workOrderTService.clickRepairForDBU(map);
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);
		}
		return null;
	}

	// 测试接口 没有业务用处
	@RequestMapping("/getSMNByESN.aspx")
	public ModelAndView getSMNByESN(HttpServletRequest request, HttpServletResponse response) throws Exception {
		try {
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			return workOrderTService.getSMNByESN(map);
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);
		}
		return null;
	}

	// 获取服务下所有待派服务技师的工单列表
	@RequestMapping("/getWaitAssignTechnicianOrderList.aspx")
	public ModelAndView getWaitAssignTechnicianOrderList(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		try {
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			return workOrderTService.getWaitAssignTechnicianOrderList(map);
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);
		}
		return null;
	}

	// 获取工单详情
	@RequestMapping("/getWorkOrderMsg.aspx")
	public ModelAndView getWorkOrderMsg(HttpServletRequest request, HttpServletResponse response) throws Exception {
		try {
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			return workOrderTService.getWorkOrderMsg(map);
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);
		}
		return null;
	}

	// 获取mt工单详情
	@RequestMapping("/getMTMsgByWorkOrder.aspx")
	public ModelAndView getMTMsgByWorkOrder(HttpServletRequest request, HttpServletResponse response,
			@RequestBody String contentJson) throws Exception {
		try {
			log.info("接口传参为"+contentJson);
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			map.put("contentJson", contentJson);
			return workOrderTService.getMTMsgByWorkOrder(map);
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);
		}
		return null;
	}

	/**
	 * 400审核通过 关闭服务工单
	 */
	@RequestMapping("/closeWorkOrderById.aspx")
	@ResponseBody
	public void closeWorkOrderById(CloseWorkOrderRequest request) {
		try {
			CloseWorkOrderCondition condition = BeanUtils.convert(request, CloseWorkOrderCondition.class);
			workOrderTService.closeWorkOrderById(condition);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
	}

	/**
	 * 400审核驳回 关闭服务工单
	 */
	@RequestMapping(value = "/refuseCloseWorkOrderById.aspx")
	@ResponseBody
	public void refuseCloseWorkOrderById(@RequestBody String request) {
		try {
			log.info("接口传参为"+request);
			RefuseCloseWorkOrderCondition condition = JSONObject.parseObject(request,
					RefuseCloseWorkOrderCondition.class);
			workOrderTService.refuseCloseWorkOrderById(condition);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
	}

	/**
	 * 工单异常关闭接口
	 *
	 */
	@RequestMapping(value = "/workOrderClosedAbnormally.aspx")
	@ResponseBody
	public ModelAndView workOrderClosedAbnormally(@RequestBody String request) {
		try {
			log.info("接口传参为"+request);
			WorkOrderCloseEntity workOrderCloseEntity = JSONObject.parseObject(request, WorkOrderCloseEntity.class);
			return workOrderTService.workOrderClosedAbnormally(workOrderCloseEntity);
		} catch (Exception e) {
			log.error(e.getMessage());
			ParameterTool.writeErrorResponse(RequestContextHolder.getResponse());
			return null;
		}
	}

	/**
	 * 移动技师     创建的单子没有技师接单时间， 导致 超时弹框弹出
	 */
	@RequestMapping(value = "/updateWorkOrder.aspx")
	@ResponseBody
	public ModelAndView updateWorkOrder(@RequestParam String ID, @RequestParam String ReceiveTimeEngineer, HttpServletResponse response) {
		try {
			Map<String, Object> map = new HashMap<>();
			map.put("ID", ID);
			map.put("ReceiveTimeEngineer", ReceiveTimeEngineer);
			return workOrderTService.updateWorkOrder(map, response);
		} catch (Exception e) {
			log.error(e.getMessage());
			ParameterTool.writeErrorResponse(RequestContextHolder.getResponse());
			return null;
		}
	}



	// sprint51需求   是否满足cds强关联    dbu服务工单强关联cds  jira：CDAP-4701
	@RequestMapping("/isMeetStrongRelationCDS.aspx")
	public ModelAndView isMeetStrongRelationCDS(HttpServletRequest request, HttpServletResponse response) throws Exception {
		try {
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			return workOrderTService.isMeetStrongRelationCDS(map);
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);
		}
		return null;
	}


	// sprint52    CDAP-4968【SRT】点击提交按钮数据对接至SF   srt项目调用此接口   将数据同步到crm
	@RequestMapping("/srtSyncRepairAckToCRM.aspx")
	public ModelAndView srtSyncRepairAckToCRM(HttpServletRequest request, HttpServletResponse response,
											@RequestBody String contentJson) throws Exception {
		try {
			log.info("接口传参为"+contentJson);
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			map.put("contentJson", contentJson);
			return workOrderTService.srtSyncRepairAckToCRM(map);
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);
		}
		return null;
	}


	/**
	 *【智能索赔】【DBU工单】【BE】OCR识别行驶证  更新工单信息
	 */
	@RequestMapping(value = "/updateWorkOrderByOcr.aspx")
	@ResponseBody
	public ModelAndView updateWorkOrderByOcr(HttpServletRequest request,HttpServletResponse response) {
		try {
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			return workOrderTService.updateWorkOrderByOcr(map, response);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
			ParameterTool.writeErrorResponse(RequestContextHolder.getResponse());
			return null;
		}
	}
}
